/*

Input: Shapefiles from $rawdata/shp

Output: 
	> $tmp/ltw18_sb_area.dta [area of precincts]
	> $newdata/ltw18_sb_mapping.dta
	> $tmp/stadtbezirke_mapping.dta

Main tasks:
	> Prepare shapefiles for mapping: precincts (Stimmbezirke) of 2018 and districts (Stadtbezirke)
	> Compute area of precincts as of 2018 
 
*/
	// To display maps correctly, Stata command "spmap" (or "grmap") requires that shp files be
	// transformed to dta files AND that any geopgraphical information (lat/lon) is projected into the same reference system.
	// "shp2dta"  or  "spshape2dta" does the first and "geo2xy" does the latter.


cd "$rawdata/shp"
clear	

 *** I Precincts ***
 
	** CREATE shape layer file (_shp.dta) and an attributes layer (data file)
	// > gens : "landtagwahl2018_stimmbezirke_shp.dta" and "landtagwahl2018_stimmbezirke.dta"
		spshape2dta  landtagwahl2018_stimmbezirke.shp, replace 
		spshape2dta  landtagwahl2018_stimmbezirke_newprj.shp, replace
		
	** PULL coordinates file: Transform geo coordinates in degrees into "Web Mercator" projection in meters (here: shp already in Mercator projection)
		use  "landtagwahl2018_stimmbezirke_shp.dta", clear
				
	 * Compute precinct area (closed polygons)
		// Generate variables for next point's coordinates
		bys _ID (shape_order): gen double next_x = _X[_n+1] 
		bys _ID (shape_order): gen double next_y = _Y[_n+1] 

		// Compute individual terms for each pair of points
		gen double term = (_X * next_y) - (next_x * _Y)

		// Compute the area for each polygon
		bys _ID: egen double polygon_area = total(term)

		// Finalize the area computation by taking the absolute value and dividing by 2
		replace polygon_area = abs(polygon_area)/2
		
		// Gen area in SQKM 
		gen  	area_sb_ltw18 = polygon_area/1000000 
		
		// KEEP unique precincts and save area data 
		collapse (mean) area_sb_ltw18, by(_ID)
		lab var area_sb_ltw18 "Precinct area in sqkm, 2018"
		
		// MERGE precinct ids 
		merge 1:m _ID using "landtagwahl2018_stimmbezirke.dta", keepusing(LTW_SB_2_1) assert(3)
		destring LTW_SB_2_1, gen(sb_new)
		duplicates drop sb_new, force
		lab var sb_new "Precinct ID, LTW 18"
		
		// SAVE: surface area, precincts 2018
		keep sb_new area_sb_ltw18
		isid sb_new 
		compress
		save "$tmp/ltw18_sb_area.dta", replace
		
		
	** PULL attribute file
		use "landtagwahl2018_stimmbezirke.dta", clear
		
		destring LTW_SB_2_1, gen(sb_new)
		
		// save precinct data for MAPs
		save "$newdata/ltw18_sb_mapping.dta", replace
		
		
 *** II. DISTRICTS ***
		* CREATE shape layer file (_shp.dta) and an attributes layer data file
		spshape2dta  stadtbezirke.shp, replace 		
		
	* PULL coordinates file: Transform geo coordinates in degrees into "Web Mercator" projection in meters
		use "stadtbezirke_shp.dta", clear

			// note: correction needed here to properly overlay precincts
			replace _X = _X +145 if !missing(_X)
			replace _Y = _Y +144 if !missing(_Y)

		save "stadtbezirke_shp.dta", replace
		
	* PULL attribute file
		use "stadtbezirke.dta", clear
		
		destring SB_NUMMER, replace
		gen stadtbez= SB_NUMMER
	
		merge 1:n _ID using  "stadtbezirke_shp.dta", assert(3) nogen
		
	 *sort: IMPORTANT to SORT otherwise there are random lines drawn!
		sort  _ID shape_order
	
	* save Stimmbezirke data for MAPs
	drop X Y
	save "$tmp/stadtbezirke_mapping.dta", replace
		
		
cd "$project"		

